package com.xuexuehai.carrecommand.controller;

import com.xuexuehai.carrecommand.dao.ICarDao;
import com.xuexuehai.carrecommand.dto.HttpResp;
import com.xuexuehai.carrecommand.entity.Car;
import com.xuexuehai.carrecommand.entity.Privs;
import com.xuexuehai.carrecommand.exception.enums.SysStatusEnums;
import com.xuexuehai.carrecommand.service.ICarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.awt.SystemColor.window;

/**
 * @author xuexuehai
 * @mailBox xuehai.xue@QQ.com
 * @date 2021/8/22 10:21 下午
 */
@Api(tags = "汽车模块")
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/car")
public class CarController {

    @Autowired
    private ICarService ics;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @ApiOperation("查询所有汽车")
    @GetMapping("/findAllCar")
    public HttpResp findAllCar() {

        List<Car> cars = null;
        cars = (List<Car>)redisTemplate.opsForValue().get("cars");

        if(cars == null){
            cars = ics.findAllCar();
            redisTemplate.opsForValue().set("cars",cars);
            redisTemplate.expire("cars",30,TimeUnit.MINUTES);
        }

        return new HttpResp(SysStatusEnums.CAR_SEARCH_SUCCESS.getCode(),
                SysStatusEnums.CAR_SEARCH_SUCCESS.getMsg(),
                cars , LocalDateTime.now());

    }

    @ApiOperation("添加车型")
    @GetMapping("/addCar")
    public HttpResp addCar(Car car){
        ics.addCar(car);
        return new HttpResp(SysStatusEnums.CAR_ADD_SUCCESS.getCode(),
                SysStatusEnums.CAR_ADD_SUCCESS.getMsg(),
                null , LocalDateTime.now());

    }

    //todo 查询品牌占比
    @ApiOperation("查询汽车车商有几种车型")
    @GetMapping("/findCarFactoryNums")
    public HttpResp findCarFactoryNums(){
        return new HttpResp(SysStatusEnums.CAR_FACTORY_NUMS_SUCCESS.getCode(),
                SysStatusEnums.CAR_FACTORY_NUMS_SUCCESS.getMsg(),
                ics.findCarFactory() ,
                LocalDateTime.now());
    }

}
